<template>
  <div class="employess">
    <el-card class="topBtn">
      <div>
        <el-button type="primary" @click="goImport">导入</el-button>
        <el-button type="primary" @click="add">+ 新增员工</el-button>
      </div>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            {{ (page.page - 1) * page.size + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              class="avatar"
              :src="row.staffPhoto"
              v-imgerror="imgerror"
              @click="imgClick(row.staffPhoto)"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column
          label="聘用形式"
          prop="formOfEmployment"
        ></el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template v-slot="{ row }">
            {{ row.timeOfEntry | filterTime }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否在职"></el-table-column> -->
        <el-table-column label="状态" prop="enableState"></el-table-column>
        <el-table-column label="操作" fixed="right" width="300">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetails(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :total="page.total"
          :current-page="page.page"
          :page-size="page.size"
          :page-sizes="[1, 5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <DialogItem @getData="getData" ref="dialogItem"></DialogItem>
    <el-dialog :visible.sync="showQrcode" title="二维码">
      <div style="text-align:center">
        <canvas ref="canvas"></canvas>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DialogItem from './components/dialogItem.vue'
import employess from '@/assets/constant/employees.js'
import { sysUser, sysUserDel } from '@/api/employess'
import QrCode from 'qrcode'
export default {
  components: {
    DialogItem
  },
  data () {
    return {
      showQrcode: false,
      list: [],
      imgerror: require('@/assets/common/head.jpg'),
      page: {
        total: 100,
        page: 1,
        size: 10
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysUser(this.page)
      // 调用结束 使用封装方法 转换数据
      res.data.rows.forEach(item => {
        // 每一项的聘用形式 都转换成调用方法得到的对应值
        item.formOfEmployment = this.formatType(+item.formOfEmployment)
      })
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 修改页码时
    handleCurrentChange (page) {
      this.page.page = page
      this.getData()
    },
    // 修改页容量时
    handleSizeChange (size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    // 封装方法用于转换传进来的数据
    formatType (formOfEmployment) {
      // 根据枚举数据信息找出传进来的数据的匹配对象
      const result = employess.hireType.find(item => {
        return item.id === formOfEmployment
      })
      // 返回 这个对象的value值
      return result ? result.value : '-'
    },
    // 新增员工
    add () {
      this.$refs.dialogItem.isShow = true
    },
    // 删除员工
    del (row) {
      this.$confirm('确定要删除吗？', '提示')
        .then(async () => {
          await sysUserDel(row.id)
          this.$message.success('删除成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    // 去导入页面
    goImport () {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    // 跳转员工详情页
    goDetails (row) {
      this.$router.push('/employees/details/' + row.id)
    },
    // 头像二维码显示
    imgClick (imgUrl) {
      if (!imgUrl) {
        console.log(1)
        this.$message.error('暂无二维码信息')
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 200,
          height: 200
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employess {
  padding: 15px;
  .topBtn {
    text-align: right;
    margin-bottom: 15px;
  }
  .page {
    text-align: right;
    padding-top: 30px;
  }
}
.avatar {
  width: 40px;
  height: 40px;
}
</style>
